﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _0712518_0712510_LTGame
{
    public class Menu : Dialog
    {
        public Texture2D[] texture;
        public Vector2 menu_direction;
        public float speed = 3f;
        public KeyboardState oldState;
        public MouseState preMState;
        public Vector2 menu_pos;
        public int mFadeIncrement;
        public int mAlphaValue;
        public int finish;
        public int type;
        public Point[,] item_pos;

        public void LoadContent(ContentManager Content)
        {
            finish = 0;
            type = 0;



            menu_direction = new Vector2(0, 220);
            menu_pos = new Vector2(100, 220);
            mFadeIncrement = 2;
            mAlphaValue = 0;
            texture = new Texture2D[4];
            texture[0] = Content.Load<Texture2D>(@"imagesMenu\menu1");
            texture[1] = Content.Load<Texture2D>(@"imagesMenu\menu2");
            texture[2] = Content.Load<Texture2D>(@"imagesMenu\menu3");
            texture[3] = Content.Load<Texture2D>(@"imagesMenu\menu4");
            item_pos = new Point[4, 2];
            item_pos[0, 0].X = 40 + (int)(menu_pos.X * 1.2f);
            item_pos[0, 0].Y = 3 + (int)(menu_pos.Y);
            item_pos[0, 1].X = 110 + (int)(menu_pos.X * 1.2f);
            item_pos[0, 1].Y = 20 + (int)(menu_pos.Y);

            item_pos[1, 0].X = 40 + (int)(menu_pos.X * 1.2f);
            item_pos[1, 0].Y = 42 + (int)(menu_pos.Y + 10);
            item_pos[1, 1].X = 144 + (int)(menu_pos.X * 1.2f);
            item_pos[1, 1].Y = 59 + (int)(menu_pos.Y + 10);

            item_pos[2, 0].X = 40 + (int)(menu_pos.X * 1.2f);
            item_pos[2, 0].Y = 82 + (int)(menu_pos.Y + 40);
            item_pos[2, 1].X = 118 + (int)(menu_pos.X * 1.2f);
            item_pos[2, 1].Y = 100 + (int)(menu_pos.Y + 40);

            item_pos[3, 0].X = 40 + (int)(menu_pos.X * 1.2f);
            item_pos[3, 0].Y = 124 + (int)(menu_pos.Y + 60);
            item_pos[3, 1].X = 148 + (int)(menu_pos.X);
            item_pos[3, 1].Y = 142 + (int)(menu_pos.Y + 60);
            oldState = Keyboard.GetState();
            preMState = Mouse.GetState();
        }
        public void Update(GameTime gameTime)
        {
            if (finish == 0)
            {
                if (menu_direction.X >= menu_pos.X)
                {
                    menu_direction.X = menu_pos.X;
                    while (mAlphaValue <= 255)
                    {
                        mAlphaValue += mFadeIncrement;
                    }
                    finish = 1;
                    return;
                }
                else
                {
                    mAlphaValue += mFadeIncrement;
                    menu_direction.X += 3f;

                }
            }
            if (finish == 1)
            {
                /***************************THAM KHAO XU LY INPUT MSDN****************************************/
                // Xy lu de tranh tinh trang key dc nhan 1 lan nhung update dc goi lai moi~ 16ms nen di chuyen thuc hien ~4 lan        
                KeyboardState newState = Keyboard.GetState();

                // Is the  key up?
                if (newState.IsKeyDown(Keys.Up))
                {
                    // If not down last update, key has just been pressed.
                    if (!oldState.IsKeyDown(Keys.Up))
                    {
                        type -= 1;
                        if (type <= 0)
                        {
                            type = 0;
                        }
                    }
                }
                else if (oldState.IsKeyDown(Keys.Up))
                {
                    // Key was down last update, but not down now, so
                    // it has just been released.
                }
                // Is the  key down?
                if (newState.IsKeyDown(Keys.Down))
                {
                    // If not down last update, key has just been pressed.
                    if (!oldState.IsKeyDown(Keys.Down))
                    {
                        type += 1;
                        if (type >= 3)
                        {
                            type = 3;
                        }
                    }
                }
                else if (oldState.IsKeyDown(Keys.Down))
                {
                    // Key was down last update, but not down now, so
                    // it has just been released.
                }

                // Update saved state.
                oldState = newState;
                /***************************THAM KHAO XU LY INPUT MSDN****************************************/
                //Mouse Event
                MouseState mouseState = Mouse.GetState();
                if (mouseState.X != preMState.X ||
                            mouseState.Y != preMState.Y)
                {
                    if (mouseState.Y >= item_pos[0, 0].Y
                         && mouseState.Y <= item_pos[0, 1].Y)
                    {
                        type = 0;
                    }
                    if (mouseState.Y >= item_pos[1, 0].Y
                      && mouseState.Y <= item_pos[1, 1].Y)
                    {
                        type = 1;
                    }
                    if (mouseState.Y >= item_pos[2, 0].Y
                       && mouseState.Y <= item_pos[2, 1].Y)
                    {
                        type = 2;
                    }
                    if (mouseState.Y >= item_pos[3, 0].Y
                       && mouseState.Y <= item_pos[3, 1].Y)
                    {
                        type = 3;
                    }
                }
                preMState = mouseState;

            }
        }



        public void Draw(SpriteBatch spriteBatch)
        {

            spriteBatch.Draw(texture[type], menu_direction, new Rectangle(0, 0, 146, 150), new Color(mAlphaValue, mAlphaValue, mAlphaValue, mAlphaValue), 0, Vector2.Zero, 1.5f, SpriteEffects.None, 0);

        }
    }
    public class Title
    {
        public Texture2D title;
        public Vector2 title_pos;
        public Vector2 title_direction;
        public int tAlphaValue;
        public int tFadeIncrement;

        public void LoadContent(ContentManager Content)
        {
            title_pos = new Vector2(150, 60);
            title_direction = new Vector2(150, 60);
            tFadeIncrement = 2;
            tAlphaValue = 0;
            title = Content.Load<Texture2D>(@"imagesMenu\title");
        }
        public int Update(GameTime gameTime)
        {

            if (tAlphaValue < 255)
            {
                tAlphaValue += tFadeIncrement;
                return 0;
            }
            else
            {
                return 1;
            }
        }


        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(title, title_direction, new Rectangle(0, 0, 321, 91), new Color(tAlphaValue, tAlphaValue, tAlphaValue, (byte)MathHelper.Clamp(tAlphaValue, 0, 255)), 0, Vector2.Zero, 1.8f, SpriteEffects.None, 0);
        }
    }
}
